//
//  XPContactCellView.m
//  QQ
//
//  Created by nhope on 2017/5/2.
//  Copyright © 2017年 xiaopin. All rights reserved.
//

#import "XPContactCellView.h"
#import "XPContactItemModel.h"

@interface XPContactCellView ()

@property (weak) IBOutlet NSImageView *avatarImageView;
@property (weak) IBOutlet NSTextField *usernameLabel;
@property (weak) IBOutlet NSTextField *latestMessageLabel;

@end

@implementation XPContactCellView

#pragma mark - Lifecycle

- (void)awakeFromNib {
    [super awakeFromNib];
    self.avatarImageView.wantsLayer = YES;
    self.avatarImageView.layer.cornerRadius = 20.0;
    self.avatarImageView.layer.masksToBounds = YES;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.usernameLabel.stringValue = @"";
    self.latestMessageLabel.stringValue = @"";
}

#pragma mark - Public

- (void)showForContactInfomation:(XPContactItemModel *)contact {
    NSString *stateString = nil;
    NSImage *avatar = [NSImage imageNamed:@"login-avatar"];
    switch (contact.state) {
        case XPUserStateOnline: stateString = @"在线"; break;
        case XPUserStateLeave: stateString = @"离开"; break;
        case XPUserStateStealth: // 隐身时显示为离线状态
        case XPUserStateOffline:
        default:
            stateString = @"离线";
            avatar = [avatar noirImage];
            break;
    }
    self.avatarImageView.image = avatar;
    self.usernameLabel.stringValue = contact.remarkName ?: contact.username;
    self.latestMessageLabel.stringValue = [NSString stringWithFormat:@"[%@] %@", stateString,contact.latestMessage];
}

@end
